package plus.stmt;

import plus.B;
import plus.Gripe;
import plus.VScope;
import plus.exp.Exp;
import plus.run.Emit;
import plus.run.Label;

public class P_ITE extends Statement {

	Exp condition;
	Statement consequence;
	Statement alternative;
	VScope ifBlock, elseBlock;

	public P_ITE(Exp condition, Statement consequence, Statement alternative) {
		this.condition = condition;
		this.consequence = consequence;
		this.alternative = alternative;
	}

	@Override
	public void threadScope() {
		Gripe.context = this; condition.threadScope();
		Gripe.context = this; condition.unify_with(B._bool);
		ifBlock = VScope.enterBlock(); consequence.threadScope(); VScope.exit();
		elseBlock = VScope.enterBlock(); alternative.threadScope(); VScope.exit();
	}

	@Override
	public void compile() {
		Label elsePart = new Label();
		Label after = new Label();
		condition.compileJcc(elsePart, false);
		Emit.pop();
		consequence.compile();
		after.JMP();
		elsePart.post();
		Emit.pop();
		alternative.compile();
		after.post();
	}
}
